package com.huluobo.a2202mvvm_im_douyindemo.mine;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.a2202mvvm_im_douyindemo.adapter.UpdateIconBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.bean.LoginBeanResult;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseViewModel;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.net.Const;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/4/22.
 */
public class MineViewModel extends BaseViewModel<MineRepo> {
    MutableLiveData<String> logoutSuccess = new MutableLiveData<>();

    MutableLiveData<String> logoutFailed = new MutableLiveData<>();

    MutableLiveData<LoginBeanResult.LoginBean> getUserIconSuccess = new MutableLiveData<>();

    MutableLiveData<String> getUserIconFailed = new MutableLiveData<>();

    MutableLiveData<UpdateIconBeanResult> updateUserIconSuccess = new MutableLiveData<>();

    MutableLiveData<String> updateUserIconFailed = new MutableLiveData<>();

    public void updateUserIcon(String filePath) {
        //1.创建文件对象
        File file = new File(filePath);
        //2.封装body
        RequestBody requestBody = RequestBody.create(MediaType.parse(Const.MEDIA_TYPE), file);
        //3.封装成part
        // 第一个参数是接口文档中的key,第二个参数是文件名,第三个参数是生成的body
        MultipartBody.Part part = MultipartBody.Part.createFormData(Const.PARAM_FILE, file.getName(), requestBody);
        repo.updateUserIcon(part, updateUserIconSuccess, updateUserIconFailed);
    }

    public void getUserIcon(String username) {
        repo.getUserIcon(username, getUserIconSuccess, getUserIconFailed);
    }

    public void logout() {
        repo.logout(logoutSuccess, logoutFailed);
    }

    @Override
    protected MineRepo createRepo() {
        return new MineRepo();
    }
}
